import axios from 'axios'
import { Message } from '@arco-design/web-vue';
import router from '@/router'
const instance = axios.create({
  baseURL: 'http://101.42.35.238:3006',
  timeout: 60000
})

//请求拦截器
instance.interceptors.request.use(
  function (config) {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.token = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  function (response) {
    if (response.data.statusCode == 11091) {
      Message.error('请重新登录!')
      localStorage.removeItem('token')

      router.push('/login')
    }
    return response
  },
  function (error) {
    if (error.response.data.statusCode == 401) {
      Message.error('请登录!')

      localStorage.removeItem('token')

      router.push('/login')
    }
  }
)

const serverUrl = 'http://101.42.35.238:3006'

export function imgs(url) {
  if (url) {
    if (url.startsWith('http')) return url
    return serverUrl + url
  } else return 'https://pic.netbian.com/uploads/allimg/240509/001628-1715184988b6fe.jpg'
}

export default instance
